package org.stone.project.logInterface.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.stone.project.common.pojo.constant.SysConstant;
import org.stone.project.common.utils.JsonUtil;
import org.stone.project.logInterface.pojo.dto.SysLog;
import org.stone.project.logInterface.service.FileLogService;
import org.stone.project.logInterface.utils.LogStrUtil;

/**
 * 实现日志文件的类
 */
@Slf4j
@Service
public class FileLogServiceImpl implements FileLogService {
    private String logFormat = "请求Id为:%s,请求时间为:%s,日志内容为:%s";

    private void addSysLog(SysLog sysLog) {
        log.info(String.format(logFormat,sysLog.getRequestId(), DateUtil.format(sysLog.getRecordDate(),SysConstant.DATE_ALL_FORMATE), JsonUtil.toJson(sysLog)));
    }

    @SneakyThrows
    @Override
    public void addLogInfoTxt(String msg,Object... msgs) {
        if (ObjectUtil.isNull(msg))
            msg = SysConstant.EMPTY_STR;
        SysLog sysLog = new SysLog(SysConstant.LOG_LEVEL_INFO,LogStrUtil.getLogStr(msg, msgs));
        addSysLog(sysLog);
    }

    @SneakyThrows
    @Override
    public void addLogWarnTxt(String msg,Object... msgs) {
        if (ObjectUtil.isNull(msg))
            msg = SysConstant.EMPTY_STR;
        SysLog sysLog = new SysLog(SysConstant.LOG_LEVEL_WARN,LogStrUtil.getLogStr(msg, msgs));
        addSysLog(sysLog);
    }

    @SneakyThrows
    @Override
    public void addLogErrTxt(String msg,Object... msgs) {
        if (ObjectUtil.isNull(msg))
            msg = SysConstant.EMPTY_STR;
        SysLog sysLog = new SysLog(SysConstant.LOG_LEVEL_ERR,LogStrUtil.getLogStr(msg, msgs));
        addSysLog(sysLog);
    }
}
